//
//  BRateView.swift
//  BMort_SwiftUI
//
//  Created by anscen on 2022/9/2.
//

import SwiftUI
import SwiftyJSON
struct BRateContentView: View {
    public   var state  :BAppMortState
    @Binding var rate   :String
    @State   var listData :[BRate] = []
    
    @Environment(\.presentationMode) var presentationMode
    var body: some View {
        VStack {
            HStack.init {
                Spacer.init(minLength: 25)
                ZStack {
                    RoundedRectangle(cornerRadius: 5).stroke().frame(height:40).foregroundColor(Color.appxdddddd())
                    HStack {
                        Spacer(minLength: 10)
                        Image(systemName: "minus.magnifyingglass").foregroundColor(.gray)
                        TextField("请输入贷款利率", text: $rate).textFieldStyle(.plain)
                    }
                }
                Spacer(minLength: 12)
                ZStack.init {
                    Button {
                        self.goBack()
                    } label: {
                        if rate.count > 0{
                            HStack {
                                Text("确定").font(.system(size: 16)).foregroundColor(.white)
                            }.padding(EdgeInsets(top: 10, leading:12, bottom: 10, trailing: 12)).background(Color.appColor()).cornerRadius(5)
                        }else{
                            
                            HStack {
                                Text("取消").font(.system(size: 16)).foregroundColor(Color.appx666666())
                            }.padding(EdgeInsets(top: 10, leading:12, bottom: 10, trailing: 12))
                        }
                    }
 
                }

                Spacer.init(minLength: 20)
            }
            List(listData, id: \.self) { model in
                Button {
                    self.rate = model.lilv ?? "4.9"
                    self.goBack()
                } label: {
                    BRateCell(model: model)
                }
            }.listStyle(.insetGrouped)
        }.onAppear {
            self.loadData()
        }.navigationBarTitle(self.state == .gjjMort ? "公积金利率" : "商业利率").navigationBarBackButtonHidden(true).navigationBarItems(leading: BaseNaviBarBack())
    }
    func loadData(){
        let value = self.state == .gjjMort ? "gjj" : "sy"
        guard let url = Bundle.main.url(forResource: "brate", withExtension: "plist") else { return  }
        guard let data = try? Data(contentsOf: url) else { return }
        guard let dic = try? PropertyListSerialization.propertyList(from: data, options: [], format: nil) else { return  }
        let json = JSON(dic)
        if let list = [BRate].deserialize(from: json[value].rawString()) as? [BRate]{
            self.listData = list
        }
        debugPrint(json)
    }
    func goBack(){
        self.presentationMode.wrappedValue.dismiss()
    }
    
}
struct BRateCell :View{
    var model :BRate
    var body: some View{
        VStack.init {
            HStack {
                Text(model.txt ?? "").font(Font(UIFont.systemFont(ofSize: 16))).foregroundColor(Color.appx000000())
                Spacer()
                Text(model.lilv ?? "").frame(width: 110, height: 36, alignment: .trailing)
            }.padding(EdgeInsets.init(top: 6, leading: 0, bottom: 6, trailing: 0))
        }
    }
}
